package com.hmdp.ThreadUtil;/**
 * @Author:fengxiaohui
 * @Description:
 * @Date:Create in 10:37 上午 2023/6/10
 * @Modified By:
 */


import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 *@Author:fengxiaohui
 *@Description:
 *@Date:Create in 10:37 上午 2023/6/10
 *@Modified By:
 */
@Slf4j
public class SaleTicketDemo01 {
    private  int num = 500;

    public synchronized void sale(){
        if (num > 0) {
            log.debug(Thread.currentThread().getName()+"--->卖出: "+(num--) +"    剩余票: "+num);
        }

    }
}
/**
* @Description: 线程操作资源类
* @Param:
* @Author: fengxiaohui
* @Date: 2023/6/10
*/
@Slf4j
 class ThreadDemo01{
    public static void main(String[] args) throws InterruptedException {
        SaleTicketDemo01 ticket = new SaleTicketDemo01();
        for (int i = 1; i <= 251 ; i++) {
            log.debug("执行次数：{}",i);
            new Thread(()->{
                ticket.sale();
            },"t1").start();
            //TimeUnit.SECONDS.sleep(2);
            new Thread(()->{
                ticket.sale();
            },"t2").start();
        }

    }
}